#pragma once
#include "ir.h"
#include <vector>
#include <map>
#include <string>

namespace cfg
{
    struct Block
    {
        std::string label;
        std::vector<ir::Instruction> instrs;
        std::vector<Block*> preds;
        std::vector<Block*> succs;
    };

    // 函数的CFG
    struct CFG
    {
        std::vector<Block> blocks;
        Block* entry_block;
    };

    class CFGGenerator
    {
    public:
        CFG Generate(ir::Function& func);
    private:
        void HandleBrBlock(Block& block);
        void HandleJmpBlock(Block& block);
        void AddEdge(Block* from, Block* to);

        CFG cfg;
        std::map<std::string, Block*> block_map;
    };
}